from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element

SGOLD_VER_HIGH="08"
SGOLD_VER_LOW="42"
SGOLD_VER_HIGH_HEX="0x08"
SGOLD_VER_LOW_HEX="0x42"
SGOLD_VER_TYPE="I"
SGOLD_VER_SVN="00.00:04.01:06.01"
S12_VERSION_ID='2.0.3_20120109'

class CSelection:
    '''This class is used to store selection of release action
    Y means do the action
    N means do NOT do the action'''
    def __init__(self):
        #do all action for the release, when this itme is Y, ignore all others
        self.All="N"
        #do update view action
        self.UpdateView="N"
        #do update version file action
        self.UpdateVF="N"
        #do generate code action
        self.Generate="N"
        #do compile code action
        self.Compile="N"
        #do save image action
        self.Save="N"
        #do backup code action
        self.BackUp="N"
        #do apple label action
        self.ApplyLabel="N"

    def Reset(self):
        #do all action for the release, when this itme is Y, ignore all others
        self.All="N"
        #do update view action
        self.UpdateView="N"
        #do update version file action
        self.UpdateVF="N"
        #do generate code action
        self.Generate="N"
        #do compile code action
        self.Compile="N"
        #do save image action
        self.Save="N"
        #do backup code action
        self.BackUp="N"
        #do apple label action
        self.ApplyLabel="N"


class CConfig:
    def __init__(self):
        #the information for different board
        self.sgold_ver_high = ""
        self.sgold_ver_low = ""
        self.sgold_ver_type = ""
        self.sgold_ver_svn = ""
        self.s12_ver_id = ""
        self.viewHome = ''
        self.partNo = ''
        self.market = ''
        #label is dictionry with format {vob: label}
        self.label = {}

        #the common information
        self.type = "NONE"
        self.saveImageLocation = "NONE"
        self.selection = CSelection()


    def Reset(self):
        self.sgold_ver_high = ""
        self.sgold_ver_low = ""
        self.sgold_ver_type = ""
        self.sgold_ver_svn = ""
        self.s12_ver_id = ""
        self.viewHome = ''
        self.partNo = ''
        self.market = ''
        self.label = {}

        self.type = "NONE"
        self.saveImageLocation = "NONE"
        self.selection.Reset()

    def UpdateFromFile(self, file):
        tree = ElementTree()
        tree.parse(file)
        dcmType = tree.find('Type')

        selection = tree.find('Selection')

        dcmList = list(tree.iter('DCM'))  #return list of all DCM
        for dcm in dcmList:
            if dcm.attrib['type'] == dcmType.text:
                break

        if dcm.attrib['type'] != dcmType.text:
            #does not find match DCM, no need to update
            return

        self.sgold_ver_high = dcm.find('VerHigh').text
        self.sgold_ver_low = dcm.find('VerLow').text
        self.sgold_ver_type = dcm.find('VerType').text
        self.sgold_ver_svn = dcm.find('SVN').text
        self.s12_ver_id = dcm.find('S12VerID').text
        self.viewHome = dcm.find('ViewHome').text
        self.type = dcm.attrib['type']
        self.saveImageLocation = tree.find('SaveImageLocation').text
        self.selection.All = selection.attrib['All']
        self.selection.UpdateView = selection.attrib['UpdateView']
        self.selection.UpdateVF = selection.attrib['UpdateVF']
        self.selection.Generate = selection.attrib['Generate']
        self.selection.Compile = selection.attrib['Compile']
        self.selection.Save = selection.attrib['Save']

    #write the current cfg to xml file
    def UpdateToFile(self,file):
        tree = ElementTree()
        tree.parse(file)

        dcmType = tree.find('Type')
        dcmType.text = self.type

        selection = tree.find('Selection')

        dcmList = list(tree.iter('DCM'))  #return list of all DCM
        for dcm in dcmList:
            if dcm.attrib['type'] == self.type:
                break

        if dcm.attrib['type'] != self.type:
            #does not find match DCM, need to create a new one
            newElement = Element('DCM', {'type': self.type})

            subViewHome = Element('ViewHome')
            subViewHome.text = self.viewHome

            subVerType = Element('VerType')
            subVerType.text = self.sgold_ver_type

            subVerLow = Element('VerLow')
            subVerLow.text = self.sgold_ver_low

            subVerHigh = Element('VerHigh')
            subVerHigh.text = self.sgold_ver_high

            subSvn = Element('SVN')
            subSvn.text = self.sgold_ver_svn

            subS12Ver = Element('S12VerID')
            subS12Ver.text = self.s12_ver_id

            newElement.append(subViewHome)
            newElement.append(subVerType)
            newElement.append(subVerLow)
            newElement.append(subVerHigh)
            newElement.append(subSvn)
            newElement.append(subS12Ver)

            tree.getroot().append(newElement)

        else:
            #has matched DCM, update it
            dcm.find('VerHigh').text = self.sgold_ver_high
            dcm.find('VerLow').text = self.sgold_ver_low
            dcm.find('VerType').text = self.sgold_ver_type
            dcm.find('SVN').text = self.sgold_ver_svn
            dcm.find('S12VerID').text = self.s12_ver_id
            dcm.find('ViewHome').text = self.viewHome
            dcm.attrib['type'] = self.type

        selection.attrib['All'] = self.selection.All
        selection.attrib['UpdateView'] = self.selection.UpdateView
        selection.attrib['UpdateVF'] = self.selection.UpdateVF
        selection.attrib['Generate'] = self.selection.Generate
        selection.attrib['Compile'] = self.selection.Compile
        selection.attrib['Save'] = self.selection.Save

        tree.write(file)




class CCfgVersion(CConfig):
    def __init_(self):
        pass
